package com.xinqi.modules.exam.paper.convert;

import com.xinqi.common.mapstruct.CommonConvertConfig;
import com.xinqi.modules.exam.common.manager.CommonClassManager;
import com.xinqi.modules.exam.common.mapstruct.IgnoreEntity;
import com.xinqi.modules.exam.paper.domain.CourseExamPaperConfigEntity;
import com.xinqi.modules.exam.paper.domain.CourseExamPaperEntity;
import com.xinqi.modules.exam.paper.domain.CourseExamPaperStatsEntity;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperCreateDTO;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperReleaseDTO;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperUpdateDTO;
import com.xinqi.modules.exam.paper.dto.rsp.CourseExamPaperBaseDTO;
import com.xinqi.modules.exam.paper.dto.rsp.CourseExamPaperConfigResultDTO;
import com.xinqi.modules.exam.paper.dto.rsp.CourseExamPaperDetailDTO;
import com.xinqi.modules.exam.paper.dto.rsp.CourseExamPaperResultDTO;
import com.xinqi.modules.exam.paper.manager.CourseExamPaperStatsManager;
import com.xinqi.modules.exam.paper.service.CourseExamPaperConfigService;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 课程测评转换
 *
 * @author: linkai
 * @date: 2023/07/02
 */
@Mapper(config = CommonConvertConfig.class)
public abstract class CourseExamPaperConvert {
    @Autowired
    private CourseExamPaperConfigService courseExamPaperConfigService;
    @Autowired
    private CourseExamPaperStatsManager courseExamPaperStatsManager;
    @Autowired
    private CommonClassManager commonClassManager;

    public abstract CourseExamPaperEntity create(CourseExamPaperCreateDTO s);

    public abstract CourseExamPaperEntity update(CourseExamPaperUpdateDTO s, @MappingTarget CourseExamPaperEntity t);

    public abstract CourseExamPaperEntity update(CourseExamPaperUpdateDTO s);

    public abstract CourseExamPaperResultDTO convert(CourseExamPaperEntity s);

    public abstract CourseExamPaperConfigEntity merge(CourseExamPaperReleaseDTO dto, @MappingTarget CourseExamPaperConfigEntity config);

    @IgnoreEntity
    public abstract CourseExamPaperEntity copy(CourseExamPaperEntity oldExamPaper);

    public abstract CourseExamPaperDetailDTO detail(CourseExamPaperEntity s);

    public abstract CourseExamPaperBaseDTO baseInfo(CourseExamPaperEntity examPaper);

    @AfterMapping
    public void after(CourseExamPaperEntity s, @MappingTarget CourseExamPaperDetailDTO t) {
        // 设置 测评配置
        CourseExamPaperConfigResultDTO config = courseExamPaperConfigService.findByExamId(s.getId());
        t.setConfig(config);
        // 设置章节名称
        if (s.getChapterId() != null) {
            t.setChapterId(s.getChapterId());
            t.setChapterName(commonClassManager.findChapterNameById(s.getChapterId()));
        }
        // 设置 测评统计
        CourseExamPaperStatsEntity stats = courseExamPaperStatsManager.findByExamId(s.getId());
        if (stats != null) {
            t.setTotalNum(stats.getTotalNum());
            t.setSubmittedNum(stats.getSubmittedNum());
            t.setApprovedNum(stats.getApprovedNum());
        }
    }

    @AfterMapping
    public void after(CourseExamPaperEntity s, @MappingTarget CourseExamPaperBaseDTO t) {
        // 设置 测评配置
        CourseExamPaperConfigResultDTO config = courseExamPaperConfigService.findByExamId(s.getId());
        t.setConfig(config);
    }
}
